import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/pages/order/order_add/model/model_add_order.dart';
import 'package:flutter_yn_goods/pages/order/order_add/model/model_order_goods.dart';
import 'package:flutter_yn_goods/user/model/app_user_info_model.dart';
import 'package:flutter_yn_goods/utils/http/http_export.dart';
import 'package:flutter_yn_goods/utils/loading/loading_utils.dart';
import 'package:flutter_yn_goods/utils/log/utils_log.dart';
import 'package:get/get.dart';

class OrderAddLogic extends GetxController {
  List<ModelAddOrder> orderGoodsRx = [
    ModelAddOrder()
  ];


  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

addOrderListener(orderGoodsRx.first);
    // orderGoodsRx.first.productFocusNode?.addListener(() {
    //   if (orderGoodsRx.first.productFocusNode?.hasFocus == false) {
    //     // 当商品输入框的键盘收起时的回调
    //     clog("商品输入框键盘已收起");
    //     // 在这里添加查询逻辑
    //   }
    // });
    // orderGoodsRx.first.memberFocusNode?.addListener(() {
    //   if (orderGoodsRx.first.memberFocusNode?.hasFocus == false) {
    //     // 当商品输入框的键盘收起时的回调
    //     clog("会员输入框键盘已收起");
    //     // 在这里添加查询逻辑
    //   }
    // });
  }


addOrderListener(ModelAddOrder addOrder){
  addOrder.productFocusNode?.addListener(() {
    if (addOrder.productFocusNode?.hasFocus == false) {
      // 当商品输入框的键盘收起时的回调
      clog("商品输入框键盘已收起");
      // 在这里添加查询逻辑
      // addOrder.pid = addOrder.productController?.text;
      if (addOrder.pid != null && addOrder.pid!.isNotEmpty) {
        getLeaderProduct(addOrder);
      }
    }
  }); 
  addOrder.memberFocusNode?.addListener(() {
    if (addOrder.memberFocusNode?.hasFocus == false) {
      // 当会员输入框的键盘收起时的回调
      clog("会员输入框键盘已收起");
      // 在这里添加查询逻辑
      // addOrder.uid = addOrder.memberController?.text;
      // clog(' addOrder.uid ${addOrder.memberController}  ${addOrder.memberController?.text  }');
      if (addOrder.uid != null && addOrder.uid!.isNotEmpty) {
        getLeaderUserInfo(addOrder);
      }
    }
  }); 
  addOrder.numFocusNode?.addListener(() {
    if (addOrder.numFocusNode?.hasFocus == false) {
      // 当数量输入框的键盘收起时的回调
      clog("数量输入框键盘已收起");
      // 在这里添加查询逻辑
      addOrder.numController?.text = addOrder.numController?.text ?? '1';
      getLeaderProduct(addOrder);
    }

  }); 
}

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();

    for (var item in orderGoodsRx) {
      item.productFocusNode?.dispose();
      item.memberFocusNode?.dispose();
      item.numFocusNode?.dispose();
      item.numController?.dispose();
    }
  }

  addOrder() async {
    // [{"uid":110011,"pid":1,"num":1},{"uid":110012,"pid":2,"num":2}]
    List datas = [];
    for (var item in orderGoodsRx) {
      if (item.userInfo == null) {
        LoadingUtils.showToast(LocaleKeys.invalid_customer_code.tr);
        return;
      }
      if (item.orderGoods == null) {
        LoadingUtils.showToast(LocaleKeys.invalid_product_code.tr);
        return;
      }
      datas.add(
          {'uid': item.uid, 'pid': item.pid, 'num': item.numController?.text});
    }

    HttpResponse response = await HttpManager().post(API.postAddOrder,
        params: {'order_info': json.encode(datas)},
        uiType: HttpUIType.loadingAndToast);
    if (response.isSuccess) {
      Get.back(result: true);
      LoadingUtils.showToast(LocaleKeys.order_entry_success.tr);
    }
  }

  getLeaderProduct(ModelAddOrder addOrder) async {
    if ((addOrder.pid ?? '').isEmpty) {
      return;
    }
    HttpResponse response = await HttpManager().get(API.getLeaderProduct,
        params: {'pid': addOrder.pid, 'num': addOrder.numController?.text ?? 1},
        uiType: HttpUIType.loadingAndToast,
        fromJson: ModelOrderGoods.fromJson);
    if (response.isSuccess) {
      addOrder.orderGoods = response.data;
      update();
    }
  }

  getLeaderUserInfo(ModelAddOrder addOrder) async {
    if ((addOrder.uid ?? '').isEmpty) {
      return;
    }
    HttpResponse response = await HttpManager().get(API.getLeaderUserInfo,
        fromJson: AppUserInfoModel.fromJson,
        params: {'uid': addOrder.uid},
        uiType: HttpUIType.loadingAndToast);
    if (response.isSuccess) {
      addOrder.userInfo = response.data;
      update();
    }
  }
}
